﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections.Specialized;

using Kerjin.Util.Google;

namespace FusionTablesClientUI
{
	public partial class frmFusionTablesClient : Form
	{
		#region "宣告全域變數"
		private string APP_PATH = "";//系統路徑
		private string LOG_PATH = "";//系統歷程記路路徑
		#endregion

		public frmFusionTablesClient()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 系統歷程
		/// </summary>
		/// <param name="message">訊息</param>
		public void SystemLogAdd(string message)
		{
			string strMessage = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + message;
			try
			{
				StreamWriter swLogFile = new StreamWriter(LOG_PATH + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".log", true, Encoding.Unicode);
				swLogFile.WriteLine(strMessage);
				swLogFile.Close();
			}
			catch { }
			if (lstLog.Items.Count >= 100)
			{
				lstLog.Items.Clear();
			}
			lstLog.Items.Add(strMessage);
			lstLog.SelectedIndex = lstLog.Items.Count - 1;
		}


		private void frmFusionTablesClient_Load(object sender, EventArgs e)
		{
			APP_PATH = Application.StartupPath + @"\";
			LOG_PATH = APP_PATH + @"Log\";
			if (!Directory.Exists(LOG_PATH))
			{
				Directory.CreateDirectory(LOG_PATH);
			}
			SystemLogAdd("程式開始執行。");
		}

		private void frmFusionTablesClient_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (MessageBox.Show("是否要關閉程式?", "詢問", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
			{
				SystemLogAdd("程式結束執行。");
			}
			else
			{
				e.Cancel = true;
			}
		}

		private void btnStart_Click(object sender, EventArgs e)
		{
			try
			{
				FusionTablesClient ftc = new FusionTablesClient();

				if (ftc.GetAuth(txtUserCode.Text, txtPassword.Text))
				{
					SystemLogAdd(ftc.Message);
					string strTableName = Guid.NewGuid().ToString();

					// 資料表欄名與資料型態
					// 請自行參考 https://developers.google.com/fusiontables/docs/developers_guide?hl=zh-tw#CreatingTable

                    List<string> aryFiled = new List<string>();
                    aryFiled.Add("FILE_BLOCK_ID:NUMBER");
                    aryFiled.Add("FILE_NAME:STRING");
                    aryFiled.Add("FILE_NAME_EXT:STRING");
                    aryFiled.Add("FILE_URL:STRING");

					// 建立資料表
                    string strTableID = ftc.CreateTable(strTableName, aryFiled);
					SystemLogAdd("Create Table => " + ftc.Message);
					if (strTableID != "")
					{
						// 取得資料表機碼資訊
						DataTable dtbTableInfo = ftc.GetTableShema(strTableID);
						SystemLogAdd("Get Table Shema => " + ftc.Message);

						// 新增資料列(執行一個SQL敘述)
						ftc.ExecuteNonQuery("INSERT INTO " + strTableID + " ('FILE_BLOCK_ID','FILE_NAME','FILE_NAME_EXT','FILE_URL') VALUES (1,'test','.txt','http://127.0.0.1/text.txt')");
						SystemLogAdd("Insert => " + ftc.Message);

						// 取得資料
						DataTable dtbData = ftc.GetData(strTableID, null);
						SystemLogAdd("Get Data = >" + ftc.Message);

						// 更新資料
						ftc.ExecuteNonQuery("UPDATE " + strTableID + " SET 'FILE_NAME'='test2' WHERE ROWID='1'");
						SystemLogAdd("Update => " + ftc.Message);

						// 取得資料
						DataTable dtbExecuteDataTable = ftc.ExecuteDataTable("SELECT * FROM " + strTableID);
						SystemLogAdd("Get Data = >" + ftc.Message);

						// 刪除資料表
						ftc.DropTable(strTableID);
						SystemLogAdd("Drop Table => " + ftc.Message);
					}
				}
				else
				{
					SystemLogAdd(ftc.Message);
				}
				ftc.Dispose();
			}
			catch (Exception exp)
			{
				SystemLogAdd(exp.Message);
			}
		}
	}
}